package com.yixun.qfbao.xf.model.enums;

import com.yixun.qfbao.util.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * @program: channel-parent
 * @description: ${description}
 * @author: Seven.wang
 * @create: 2020-07-31 14:19
 **/
public enum TimeSearchCriteriaEnum implements Serializable {

    TODAY("1", "今日"),
    THREE_DAY("2", "近3日"),
    SEVEN_DAY("3", "近7日"),
    A_MONTH("4", "近1个月"),
    THREE_MONTH("5", "近3个月");

    private final String no;

    private final String des;

    TimeSearchCriteriaEnum(String no, String des) {
        this.no = no;
        this.des = des;
    }

    public String getNo() {
        return no;
    }

    public String getDes() {
        return des;
    }

    public static TimeSearchCriteriaEnum getByDescription(String des) {
        if (StringUtils.isBlank(des)) {
            return null;
        }
        for (TimeSearchCriteriaEnum menum : values()) {
            if (menum.getDes().equals(des)) {
                return menum;
            }
        }
        return null;
    }

    public static TimeSearchCriteriaEnum getByNo(String no) {
        if (StringUtils.isBlank(no)) {
            return null;
        }
        for (TimeSearchCriteriaEnum menum : values()) {
            if (menum.getNo().equals(no)) {
                return menum;
            }
        }
        return null;
    }

    public static String getDescByNo(String no) {

        TimeSearchCriteriaEnum byNo = getByNo(no);

        return byNo == null ? null : byNo.getDes();

    }

    /**
     *  日期检索条件枚举转换为对应日期
     * @param no
     * @return
     */
    public static Date getDateByNo(String no) {
        TimeSearchCriteriaEnum byNo = getByNo(no);
        Date date = null;
        Date today = DateUtils.getCurrentTime();
        switch (byNo){
            case TODAY:
                date = today;
                break;
            case THREE_DAY:
                date = DateUtils.addDay(today, -3);
                break;
            case SEVEN_DAY:
                date = DateUtils.addDay(today, -7);
                break;
            case A_MONTH:
                date = DateUtils.addMonth(today, -1);
                break;
            case THREE_MONTH:
                date = DateUtils.addMonth(today, -3);
                break;
        }
        return date;

    }
}
